package org.jeecg.modules.front.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.front.entity.vo.*;
import org.jeecg.modules.gongqiang.entity.*;
import org.jeecg.modules.gongqiang.service.*;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/front/good")
@Api(tags = "前台用户基础模块")
@Slf4j
public class FrontGoodController {
    @Autowired
    private IGoodTypeService goodTypeService;
    @Autowired
    private IGoodService goodService;
    @Autowired
    private IGoodDetailService goodDetailService;
    @Autowired
    private ISerialNumberService serialNumberService;
    @Autowired
    private IUserhomeOrderService userhomeOrderService;

    @ApiOperation("商品分类")
    @RequestMapping(value = "/type", method = RequestMethod.GET)
    public Result<List<GoodType>> type(HttpServletRequest req) {
        LambdaQueryWrapper<GoodType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(GoodType::getSsort);
        List<GoodType> goodTypeList = goodTypeService.list(queryWrapper);
        return Result.ok(goodTypeList);
    }

    @ApiOperation("商品列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<Good>> list(
            @RequestParam(name = "goodTypeId", required = false) String goodTypeId,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {

        LambdaQueryWrapper<Good> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(goodTypeId)) {
            queryWrapper.eq(Good::getGoodTypeId, goodTypeId);
        }
        queryWrapper.eq(Good::getStatus, 1);
        queryWrapper.orderByAsc(Good::getIsort);
        queryWrapper.orderByDesc(Good::getCreateTime);
        Page<Good> page = new Page<>(pageNo, pageSize);
        IPage<Good> goodList = goodService.page(page, queryWrapper);
        return Result.ok(goodList);
    }

    @ApiOperation("商品详情")
    @RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
    public Result<JSONObject> show(@PathVariable("id") String id, HttpServletRequest req) {
        Result<JSONObject> result = new Result<>();
        Good good = goodService.getById(id);
        if (good == null) {
            result.error500("商品已消失");
            return result;
        }
        if (!good.getStatus().equals(1)) {
            result.error500("商品已下架");
            return result;
        }
        LambdaQueryWrapper<GoodDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodDetail::getGoodId, id);
        queryWrapper.orderByAsc(GoodDetail::getTermId);
        List<GoodDetail> goodDetailList = goodDetailService.list(queryWrapper);
        JSONObject obj = new JSONObject(new LinkedHashMap<>());
        obj.put("good", good);
        obj.put("goodDetail", goodDetailList);
        result.setResult(obj);
        result.success("获取成功");
        return result;
    }

    @ApiOperation("我的订单")
    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public Result<IPage<FrontUserhomeOrder>> order(
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        Page<FrontUserhomeOrder> page = new Page<>(pageNo, pageSize);
        IPage<FrontUserhomeOrder> goodList = goodService.pageIndexList(page, status, userId);
        for (FrontUserhomeOrder frontUserhomeOrder : goodList.getRecords()) {
            LambdaQueryWrapper<SerialNumber> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SerialNumber::getOrderNum, frontUserhomeOrder.getOrderNum());
            List<SerialNumber> serialNumberList = serialNumberService.list(queryWrapper);
            frontUserhomeOrder.setSerialNumberList(serialNumberList);
        }
        return Result.ok(goodList);
    }

    @ApiOperation("我的订单详情")
    @RequestMapping(value = "/order/{orderId}", method = RequestMethod.GET)
    public Result<FrontUserhomeOrder> orderShow(
            @PathVariable("orderId") String orderId,
            HttpServletRequest req) {
        FrontUserhomeOrder good = goodService.pageIndex(orderId);
        if(good != null){
            LambdaQueryWrapper<SerialNumber> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SerialNumber::getOrderNum, good.getOrderNum());
            List<SerialNumber> serialNumberList = serialNumberService.list(queryWrapper);
            good.setSerialNumberList(serialNumberList);
        }
        return Result.ok(good);
    }

    @ApiOperation("取消订单")
    @RequestMapping(value = "/order/{orderId}", method = RequestMethod.POST)
    public Result<FrontUserhomeOrder> orderCancel(
            @PathVariable("orderId") String orderId,
            HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        userhomeOrderService.cancelOrder(orderId,userId);
        return Result.ok("取消成功");
    }
}